package com.situ.library.service.impl;

import com.situ.library.dao.IBookDao;
import com.situ.library.dao.impl.BookDaoImpl;
import com.situ.library.listener.BookExcelListener;
import com.situ.library.pojo.entity.Book;
import com.situ.library.pojo.query.BookQuery;
import com.situ.library.pojo.vo.BookVO;
import com.situ.library.service.IBookService;
import com.situ.library.util.ExcelUtils;
import com.situ.library.util.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class BookServiceImpl implements IBookService {
    private IBookDao bookDao = new BookDaoImpl();

    @Override
    public PageInfo selectByPage(BookQuery bookQuery) {
        List<BookVO> list = bookDao.selectByPage(bookQuery);
        //现在要搜索的是符合条件的总的数量
        int totalCount = bookDao.selectTotalCount(bookQuery);

        return  PageInfo.ok(totalCount, list);
    }

    @Override
    public void deleteById(int id) {
        bookDao.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        //delete from book where id in(2, 4, 5);
        for (String id : ids) {
            bookDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Book book) {
        bookDao.add(book);
    }

    @Override
    public void update(Book book) {
        bookDao.update(book);
    }

    @Override
    public Book selectById(int id) {
        return bookDao.selectById(id);
    }

    @Override
    public List<Book> selectAll() {
        return bookDao.selectAll();
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<Book> list = bookDao.selectAll();
        ExcelUtils.exportExcel(resp, list, Book.class, "book数据");
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtils.importExcel(req, new BookExcelListener(), Book.class);
    }
}
